package cn.mzy.delay;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

//消息发送者
public class Producer {

    public static void main(String[] args) {
        try {
            // 实例化消息生产者Producer
            DefaultMQProducer producer = new DefaultMQProducer("sync_group_name");
            // 设置NameServer的地址
            producer.setNamesrvAddr("localhost:9876");
            // 启动Producer实例
            producer.start();
            //构建消息
            Message message = new Message("delay_order_topic", "delay_pet_tag", ("我是消息").getBytes(StandardCharsets.UTF_8));
            message.setDelayTimeLevel(3);
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
            System.out.println(sendResult.getSendStatus());
            // 如果不再发送消息，关闭Producer实例。
            producer.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}